<?php
namespace App\HttpController\Admin;

use App\Repositories\Contracts\BaseOperateRepository;
use App\Repositories\Eloquent\Admin\SysNodeModel;
use EasySwoole\Template\Render;

class Node extends Sys implements BaseOperateRepository {

    private  $_model;

    public function index()
    {
        $this->show('admin/node/index',['pid'=>0]);
    }

    public function getList()
    {
        $this->_model = new SysNodeModel();
        $where = [];
        if (!empty($this->_params['key'])) {
            $id = $this->_params['key']['id'];
            $name = $this->_params['key']['name'];
            if($id){
                $where['id'] = $id;
            }
            if($name){
                $where['name'] = ['%' . $name . '%', 'like'];
            }

            if(!empty($this->_params['key']['level']))
            {
                $level = $this->_params['key']['level'];
                $where['level'] = [explode(',',$level),'in'];
            }
            if (!empty($this->_params['key']['path'])) {
                $path = $this->_params['key']['path'];
                $where['path'] = ['%' . $path . '%','like'];
            }
        }
        if (!empty($this->_params['pid'])) {
            $where['pid'] = $this->_params['pid'];
        }
        $where['status'] = [-1,'>'];
        $where['deleted'] = 0;
        $datas = $this->_model->getList($where,$pageNum = 2000);

        $this->ajaxSuccess($datas['datas'],'',$datas['count']);
    }

    public function add()
    {
        $pid = 0;
        if (!empty($this->_params['pid'])) {
            $pid = $this->_params['pid'];
        }
        $this->show('admin/node/add',['pid'=>$pid]);
    }

    public function add_ajax()
    {
        $this->_model = new SysNodeModel();

        $pid = 0;
        if (!empty($this->_params['pid'])) {
            $pid = $this->_params['pid'];
        }


        $data = $this->_params;
        $data['path'] = intval(0);
        if ($pid > 0) {
            //验证节点
            $item_pid = $this->_model->getItemById($pid);
            if (!empty($item_pid) && $item_pid['id'] > 0) {
                $data['pid'] = $item_pid['id'];
                $data['path'] = $item_pid['path'] . ',' . $item_pid['id'];
                $data['level'] = (intval($item_pid['level']) + 1);
            }
        }

        $data['add_time'] = time();
        $reslut = $this->_model->addItem($data);
        if ($reslut) {
            $data_no['no'] = $data['path'] . ',' . $reslut;
            $this->_model->updateItemById($reslut,$data_no);
            $this->ajaxSuccess();
        }

        $this->ajaxError();

    }

    public function edit()
    {
        $this->_model = new SysNodeModel();
        $param = $this->_params;
        $id = 0;
        if (!empty($param['id'])) {
            $id = $param['id'];
        } else {
           $this->ajaxError(0,"参数错误");
        }
        $item = array();
        if ($id > 0) {
            $tmp['id'] = $id;
            $item = $this->_model->getItemByid($id);
        }
        $this->show('admin/node/edit',['item'=>$item]);

    }

    public function edit_ajax()
    {
        $this->_model = new SysNodeModel();
        $id = $this->_params['id'];
        if ($id > 0) {
            unset($this->_params['id']);
            $item = $this->_params;
            $reslut = $this->_model->updateItemById($id, $item);
            if ($reslut) {
                $this->ajaxSuccess();
            }
            $this->ajaxError(0,"参数错误");
        }
    }

    public function status()
    {
        $this->_model = new SysNodeModel();
        $param = $this->_params;
        if (!empty($param['ids']) && !is_null($param['status'])) {
            $ids = $param['ids'];
            //-1 删除 0禁用 1 正常启用
            $status = $param['status'];
        } else {
            $this->ajaxError(0,"参数错误");
        }
        if ($status < -2 && $status > 1) {
            $this->ajaxError(0,"参数错误");
        }
        if (!$ids) {
            $id = I('id');
            if ($id) {
                $ids [] = $id;
            }
        }
        if (!$ids) {
            $this->ajaxError(0,"您未选择任何记录");
        }
        $ids = explode(',', $ids);
        $data['status'] = $status;
        $result = $this->_model->status($ids, $data);
        $result? $this->ajaxSuccess():$this->ajaxError(0,"操作失败");
    }

    public function del()
    {
        $this->_model = new SysNodeModel();
        $id = $this->_params['id'];
        if (empty($id)) {
            $this->ajaxError(0,"参数错误");
        }
        $result = $this->_model->delById($id);
        $result? $this->ajaxSuccess():$this->ajaxError(0,"操作失败");

    }

    public function sort()
    {
        $this->_model = new SysNodeModel();
        $id = $this->_params['id'];
        if (empty($id)) {
            $this->ajaxError(0,"参数错误");
        }
        $result = $this->_model->updateItemById($id,['px'=>$this->_params['px']]);
        $result ? $this->ajaxSuccess():  $this->ajaxError(0,"操作失败");
    }

    function reload(){
        Render::getInstance()->restartWorker();
        $this->response()->write(1);
    }

    protected function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
        $this->_model = null;
        $this->_mode_node = null;
    }


}
